/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import datamodel.Message;
import view.commands.EmptyCommand;

/**
 *
 * @author Kvest
 */
public class MessageForm extends BasicForm
{
    private static final int BACK_COMMAND_ID = 1;
    private static final int OK_COMMAND_ID = 2;
    
    private TextArea text;
    private Message message;
      
    private int tag;
    
    public MessageForm()
    {
        super();
        
        tag = -1;
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
        
        text = new TextArea("", 1, 30);
        text.setEditable(false);
        text.setGrowByContent(true); 
        addComponent(text);

        //Команды
        addCommand(new EmptyCommand());
        
        Command backCommand = new Command("back", BACK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                if (!message.isRead())
                {
                    message.setRead(true);
                    IMAPMIDlet.getMidlet().getMessageManager().messageWasRead(message);
                } 
      
                ScreenEngine.getEngine().pop(true);
                evt.consume();
            }
        };
        addCommand(backCommand);
        
        Command okCommand = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                if (!message.isRead())
                {
                    message.setRead(true);
                    IMAPMIDlet.getMidlet().getMessageManager().messageWasRead(message);
                }    
                        
                ScreenEngine.getEngine().pop(true);
                evt.consume();
            }
        };
        addCommand(okCommand);
    }
    
    public void setMessage(Message message)
    {
        this.message = message;
        text.setText(this.message.getReciveDate() + "\n\n" + this.message.getMessage());
    }

    public int getTag() 
    {
        return tag;
    }

    public void setTag(int tag) 
    {
        this.tag = tag;
    }
}
